package com.vision.sales.monthlytotals.cl.ui;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiFactory;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.core.client.Style.SelectionMode;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.widget.core.client.TabItemConfig;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.form.DateField;
import com.sencha.gxt.widget.core.client.grid.ColumnConfig;
import com.sencha.gxt.widget.core.client.grid.ColumnModel;
import com.sencha.gxt.widget.core.client.grid.Grid;
import com.sencha.gxt.widget.core.client.grid.GridView;
import com.sencha.gxt.widget.core.client.toolbar.LabelToolItem;
import com.vision.core.cl.Callback;
import com.vision.core.cl.ui.TabItemPanel;
import com.vision.core.cl.ui.UI;
import com.vision.core.cl.ui.component.BasicGridView;
import com.vision.sales.cl.SalesServices;
import com.vision.sales.cl.SalesTasks;
import com.vision.sales.cl.data.MonthlyTotalsItemProperties;
import com.vision.sales.cl.resource.SalesResources;
import com.vision.sales.cm.data.MonthlyTotals;
import com.vision.sales.cm.data.MonthlyTotalsItem;

/**
 * 
 * @author Mark
 *
 */
public class MonthlyTotalsPanel extends TabItemPanel {
	
	interface Binder extends UiBinder<Widget, MonthlyTotalsPanel> { }

	private static final Binder binder = GWT.create(Binder.class);
	
	@UiField VerticalLayoutContainer mainPanel;
	@UiField DateField monthYear;
	@UiField LabelToolItem noOfDays;
	@UiField Grid<MonthlyTotalsItem> grid;
	
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	public MonthlyTotalsPanel() {
		setWidget(binder.createAndBindUi(this));
		
		monthYear.setPropertyEditor(UI.dateEditor("MMM yyyy"));
		monthYear.setValue(new Date(), false);
	}

	// UiFactory Methods %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	/**
	 * Used to create the 'grid' field in the XML file. 
	 * 
	 * @return
	 */
	@UiFactory
	Grid<MonthlyTotalsItem> initializeGrid() {
		MonthlyTotalsItemProperties props = MonthlyTotalsItemProperties.INSTANCE;
		
		List<ColumnConfig<MonthlyTotalsItem, ?>> columns = new ArrayList<ColumnConfig<MonthlyTotalsItem, ?>>();
		
		columns.add(UI.column(props.transaction(), 400, "Transactions"));
		columns.add(UI.column(props.total(), 100, "Total", UI.amountCell(), HasHorizontalAlignment.ALIGN_RIGHT));
		columns.add(UI.column(props.average(), 100, "Average", UI.amountCell(), HasHorizontalAlignment.ALIGN_RIGHT));
		columns.add(UI.column(props.minimum(), 100, "Min", UI.amountCell(), HasHorizontalAlignment.ALIGN_RIGHT));
		columns.add(UI.column(props.maximum(), 100, "Max", UI.amountCell(), HasHorizontalAlignment.ALIGN_RIGHT));
		
		ColumnModel<MonthlyTotalsItem> cm = new ColumnModel<MonthlyTotalsItem>(columns);
		
		GridView<MonthlyTotalsItem> view = new BasicGridView<MonthlyTotalsItem>();
		view.setEmptyText("There is no data to display.");

		Grid<MonthlyTotalsItem> grid = new Grid<MonthlyTotalsItem>(new ListStore<MonthlyTotalsItem>(MonthlyTotalsItemProperties.INSTANCE.key()), cm, view);
		grid.setAllowTextSelection(false);
		grid.getSelectionModel().setSelectionMode(SelectionMode.MULTI);
		
		return grid;
	}
	
	// Logic %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	@UiHandler("monthYear")
	public void onDateChanged(ValueChangeEvent<Date> event) {
		loadData();
	}
	
	// Listeners %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


	// ITabItemPanel Methods %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	@Override
	public String getPanelId() {
		return SalesTasks.MONTHLY_TOTALS.getId();
	}
	
	@Override
	public TabItemConfig getConfig() {
		if (config == null) {
			config = new TabItemConfig("Monthly Totals", true);
			config.setIcon(SalesResources.INSTANCE.monthlyTotals());
		}
		return config;
	}
	
	@Override
	public void loadData() {
		if (Log.isDebugEnabled())
			Log.debug("Loading monthly totals for " + monthYear.getText());
		
		mainPanel.mask("Loading monthly totals for " + monthYear.getText());
		SalesServices.monthlyTotalsSvc().getMonthlyTotals(monthYear.getValue(), null, null, new Callback<MonthlyTotals>(mainPanel) {
			@Override
			protected void handleSuccess(MonthlyTotals result) {
				noOfDays.setLabel("No of Days: " + result.getNumberOfDays());
				noOfDays.setVisible(true);
				
				grid.getStore().clear();
				grid.getStore().addAll(result.getItems());
			}
		});
	}
	
}
